package com.yuanrui.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuanrui.auth.application.convert.AuthPermissionConverter;
import com.yuanrui.auth.application.dto.AuthPermissionDTO;
import com.yuanrui.auth.common.entity.Result;
import com.yuanrui.auth.domain.entity.AuthPermissionBO;
import com.yuanrui.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限控制器
 */
@RestController
@Slf4j
@RequestMapping("/permission/")
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 添加权限
     */
    @RequestMapping("add")
    public Result<Boolean> addRole(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.add：{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convert(authPermissionDTO);
            Boolean addPermission = authPermissionDomainService.addPermission(authPermissionBO);
            return Result.ok(addPermission);
        } catch (Exception e) {
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return Result.fail("添加权限失败");
        }
    }

    /**
     * 修改权限
     */
    @RequestMapping("update")
    public Result<Boolean> updateRole(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.update：{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convert(authPermissionDTO);
            Boolean updatePermission = authPermissionDomainService.updatePermission(authPermissionBO);
            return Result.ok(updatePermission);
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改权限失败");
        }
    }

    /**
     * 删除权限
     */
    @RequestMapping("delete")
    public Result<Boolean> deleteRole(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isDebugEnabled()) {
                log.debug("RoleController.delete：{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionConverter.INSTANCE.convert(authPermissionDTO);
            Boolean deletePermission = authPermissionDomainService.deletePermission(authPermissionBO);
            return Result.ok(deletePermission);
        } catch (Exception e) {
            log.error("RoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除权限失败");
        }
    }

    /**
     * 查询权限
     */
    @RequestMapping("getPermission")
    public Result<String> getPermission(String userName) {
        try {
            log.info("RoleController.getPermission.userName:{}", userName);
            Preconditions.checkNotNull(userName, "用户名不能为空");
            List<String> permission = authPermissionDomainService.getPermission(userName);
            return Result.ok(permission);
        } catch (Exception e) {
            log.error("RoleController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询权限失败");
        }
    }

    /**
     * 修改用户权限
     */
    @RequestMapping("updatePermission")
    public Result<Boolean> updatePermission(String userName, String roleId) {
        try {
            log.info("RoleController.updatePermission.userName:{},roleId:{}", userName, roleId);
            Preconditions.checkNotNull(userName, "用户名不能为空");
            Preconditions.checkNotNull(roleId, "角色ID不能为空");
            Boolean userPermission = authPermissionDomainService.updateUserPermission(userName, roleId);
            return Result.ok(userPermission);
        } catch (Exception e) {
            log.error("RoleController.updatePermission.error:{}", e.getMessage(), e);
            return Result.fail("修改用户权限失败");
        }
    }
}
